(function(angular) {
	'use strict';
	angular.module('fs.waf').controller('cda.transaction.verifyController', verifyController);
	verifyController.$inject = [ '$scope', 'Constants', 'modalService', 'layoutConfig', 'validatorConfig', 'gridService', 'authService', 'transactionService' ];
	function verifyController($scope, Constants, modalService, layoutConfig, validatorConfig, gridService, authService, transactionService) {
		var ctrl = this, eventQ = $scope.$stateParams.params;
		ctrl.layoutConfig = layoutConfig;
		ctrl.validatorConfig = validatorConfig;
		ctrl.txn = {};
		
		ctrl.init = function() {
			var params = {
					wfItemKey : eventQ.wfItemKey
			};
			return authService.verify(params).then(function(response) {
				transactionService.dataFilter().then(function(result){
					ctrl.transactionStatusFilter = result.transactionStatusFilter;
					ctrl.transactionActionFilter = result.transactionActionFilter;
					ctrl.transactionPayFilter = result.transactionPayFilter;
					ctrl.txn = response.data || eventQ;
					ctrl.txn.eventWfItemId = eventQ.wfItemId;
				});	
			}, angular.noop);
		};
		
		ctrl.verifyApprove = function() {
			modalService.openConfirmDialogs({
				'messages' : [ {msg : 'title.dialog.confirm.approve', args : [ctrl.txn.primaryKey]} ],
				'btnCallback' : {
					'ok' : function() {
						authService.verifyApprove(ctrl.txn).then(approveSuccessful, approveFailed);
					}
				}
			});
		};

		ctrl.verifyReject = function() {
			modalService.openPromptDialogs({
				'btnCallback' : {
					'ok' : function(comments) {
						ctrl.txn.comments = comments;
						authService.verifyReject(ctrl.txn).then(rejectSuccessful, rejectFailed);
					}
				}
			});
		};
		
		function rejectSuccessful(response) {
			modalService.openAcknowledgeDialogs({
				'messages' : [ 'title.dialog.successful.reject' ],
				'btnCallback' : {
					'close' : function() {
						$scope.go('cda.eventWf.list');
					}
				},
				'redirections' : [ {
					'name' : 'title.infobar.eventwf.list',
					'state' : 'cda.eventWf.list',
					'params' : {}
				}, {
					'name' : 'title.infobar.transaction.list',
					'state' : 'cda.transaction.list',
					'params' : {}
				}]
			});
		}

		function rejectFailed(response) {
			modalService.openErrorDialogs({
				'messages' : response.error
			});
		}

		function approveSuccessful(response) {
			modalService.openAcknowledgeDialogs({
				'messages' : [ 'title.dialog.successful.approve' ],
				'btnCallback' : {
					'close' : function() {
						$scope.go('cda.eventWf.list');
					}
				},
				'redirections' : [ {
					'name' : 'title.infobar.eventwf.list',
					'state' : 'cda.eventWf.list',
					'params' : {}
				}, {
					'name' : 'title.infobar.transaction.list',
					'state' : 'cda.transaction.list',
					'params' : {}
				}]
			});
		}

		function approveFailed(response) {
			modalService.openErrorDialogs({
				'messages' : response.error
			});
		}
		
	}
})(angular);